package com.whfc.openapi.api.face.controller;

import com.alibaba.fastjson.JSON;
import com.whfc.common.face.fcr.enums.FcrApplyCode;
import com.whfc.common.face.fcr.enums.FcrOp;
import com.whfc.common.face.fcr.enums.FcrRequestType;
import com.whfc.common.face.fcr.param.FcrRecordParam;
import com.whfc.common.face.fcr.param.FcrSyncDoorParam;
import com.whfc.common.face.fcr.param.FcrSyncEmpParam;
import com.whfc.common.face.fcr.param.FcrSyncParam;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.whfc.emp.param.EmpDeviceSyncDataParam;
import com.whfc.emp.param.EmpInfoSyncDataParam;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import com.whfc.openapi.api.face.util.FaceGateDataConverter;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 闸机数据同步(深圳玉川)
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020/9/10 14:17
 */
@RestController
@RequestMapping("/open/api/faceGate/fcr")
public class FaceGateFcrController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaceGateSyncService faceGateSyncService;

    @PostMapping("callback")
    public String callback(@RequestBody String body, @RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.debug("闸机数据同步body:{}", body);
        Integer deptId = clientDTO.getDeptId();
        FcrSyncParam param = JSON.parseObject(body, FcrSyncParam.class);
        logger.info("闸机数据同步,deptId:{},body:{}", deptId, param);
        if (param != null) {
            param.setDeptId(deptId);
            String op = param.getOp();
            String data = param.getData();
            if (FcrOp.PASS_RECORD.getValue().equals(op)) {
                //同步考勤记录
                FcrRecordParam fcrRecordParam = JSON.parseObject(data, FcrRecordParam.class);
                logger.info("fcr考勤同步,deptId:{},{}", deptId, fcrRecordParam);
                EmpAttendSyncDataParam dataParam = FaceGateDataConverter.attendDataConverter(fcrRecordParam);
                if (dataParam != null) {
                    dataParam.setDeptId(deptId);
                    dataParam.setPlatform(FaceGateType.SZYC.getCode());
                    faceGateSyncService.syncAttend(dataParam);
                }
            } else if (FcrOp.DOOR.getValue().equals(op)) {
                //同步设备
                FcrSyncDoorParam fcrSyncDoorParam = JSON.parseObject(data, FcrSyncDoorParam.class);
                EmpDeviceSyncDataParam dataParam = FaceGateDataConverter.deviceDataConverter(fcrSyncDoorParam);
                if (dataParam != null) {
                    dataParam.setDeptId(deptId);
                    dataParam.setPlatform(FaceGateType.SZYC.getCode());
                    faceGateSyncService.syncDevice(dataParam);
                }
            } else if (FcrOp.EMP.getValue().equals(op) || FcrOp.DEL_EMP.getValue().equals(op)) {
                //同步人员信息
                FcrSyncEmpParam fcrSyncEmpParam = JSON.parseObject(data, FcrSyncEmpParam.class);
                EmpInfoSyncDataParam dataParam = FaceGateDataConverter.empInfoDataConverter(fcrSyncEmpParam, op);
                if (dataParam != null) {
                    dataParam.setDeptId(deptId);
                    dataParam.setPlatform(FaceGateType.SZYC.getCode());
                    faceGateSyncService.syncEmpInfo(dataParam);
                }
            }
        } else {
            param = new FcrSyncParam();
        }
        // 应答
        param.setCode(FcrApplyCode.SUCCESS.getValue());
        param.setRequestType(FcrRequestType.APPLY.getValue());
        param.setMessage(FcrApplyCode.SUCCESS.getDesc());
        return JSON.toJSONString(param);
    }

}
